<?php

namespace app\api\controller\hotel;

use app\common\controller\Api;
use think\Hook;
use app\api\controller\hotel\Token;

/**
 * 获取数据
 */
class Base extends Api
{
    
    protected $url = '';
    protected $request_data = '';
    protected $table = '';
    protected $header = '';
    
    
    public function _initialize()
    {
        $this->table_obj = new Init();
        parent::_initialize();
    }
   
    protected function commonDealData($key = ''){
        
        try{
            $table = $this->table;       
            $query = http_build_query($this->request_data);
            $response = httpCurlpost($this->url .$query ,$this->request_data,$this->header);
            $result = json_decode($response,true);
            if($result['code'] != 0)return $result['msg'];
            if(empty($key)){
               $ruku_data = $result['data'];
            }else{
                $ruku_data = $result['data'][$key];
            }
            
            if(empty($ruku_data) || empty($ruku_data[0])) return '接口无数据';
 
            $create_table = $this->table_obj->createTableByData($ruku_data[0],$table);
            
            if($create_table === TRUE){
                $field_list = $this->table_obj->get_table_field(config('database.prefix').$table);
                array_shift($field_list);
                array_pop($field_list);
                $list = [];
                $exist_list = db($table)->column('md5');
                foreach ($ruku_data as $value){
                    $middle_data = [];
                    foreach ($value as $k=>$v){
                        $n_k = strtolower($k);
                        $middle_data[$n_k] = $v;
                    }

                    $temp = [];
                    foreach ($field_list as $v){
                        $temp[] = empty($middle_data[$v])?'':$middle_data[$v];
                    }
                    $md5 = fyencrypt(implode('', $temp));
                    if(in_array($md5, $exist_list)){
                        continue;
                    }
                    $middle_data['md5'] = $md5;

                    $list[] = $middle_data;
                }            
                if(!empty($list)){
                    db($table)->insertAll($list);
                }
                return true;
            }
            return $create_table;
        }catch (Exception $e) {
            return $e->getMessage();
        }
    }
    
   
       

    
}
